/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIESREGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability 
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : r_cg_sci.c
* Version      : Code Generator for RX23T V1.00.01.01 [30 Oct 2015]
* Device(s)    : R5F523T5AxFM
* Tool-Chain   : CCRX
* Description  : This file implements device driver for SCI module.
* Creation Date: 2017-5-9
***********************************************************************************************************************/

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_sci.h"
/* Start user code for include. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
uint8_t * gp_sci1_tx_address;               /* SCI1 transmit buffer address */
uint16_t  g_sci1_tx_count;                  /* SCI1 transmit data number */
uint8_t   g_sci5_iic_transmit_receive_flag; /* SCI5 transmit receive flag for I2C */
uint8_t   g_sci5_iic_cycle_flag;            /* SCI5 start stop flag for I2C */
uint8_t   g_sci5_slave_address;             /* SCI5 target slave address */
uint8_t * gp_sci5_tx_address;               /* SCI5 transmit buffer address */
uint16_t  g_sci5_tx_count;                  /* SCI5 transmit data number */
uint8_t * gp_sci5_rx_address;               /* SCI5 receive buffer address */
uint16_t  g_sci5_rx_count;                  /* SCI5 receive data number */
uint16_t  g_sci5_rx_length;                 /* SCI5 receive data length */
/* Start user code for global. Do not edit comment generated here */
extern volatile uint8_t   send_end;
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
* Function Name: R_SCI1_Create
* Description  : This function initializes SCI1.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Create(void)
{
    /* Cancel SCI1 module stop state */
    MSTP(SCI1) = 0U;

    /* Set interrupt priority */
    IPR(SCI1, ERI1) = _0F_SCI_PRIORITY_LEVEL15;

    /* Clear the control register */
    SCI1.SCR.BYTE = 0x00U;

    /* Set clock enable */
    SCI1.SCR.BYTE = _00_SCI_INTERNAL_SCK_UNUSED;

    /* Clear the SIMR1.IICM, SPMR.CKPH, and CKPOL bit, and set SPMR */
    SCI1.SIMR1.BIT.IICM = 0U;
    SCI1.SPMR.BYTE = _00_SCI_RTS | _00_SCI_CLOCK_NOT_INVERTED | _00_SCI_CLOCK_NOT_DELAYED;

    /* Set control registers */
    SCI1.SMR.BYTE = _00_SCI_CLOCK_PCLK | _00_SCI_STOP_1 | _00_SCI_PARITY_EVEN | _00_SCI_PARITY_DISABLE | 
                    _00_SCI_DATA_LENGTH_8 | _00_SCI_MULTI_PROCESSOR_DISABLE | _00_SCI_ASYNCHRONOUS_MODE;
    SCI1.SCMR.BYTE = _00_SCI_SERIAL_MODE | _00_SCI_DATA_INVERT_NONE | _00_SCI_DATA_LSB_FIRST | 
                     _10_SCI_DATA_LENGTH_8_OR_7 | _62_SCI_SCMR_DEFAULT;
    SCI1.SEMR.BYTE = _10_SCI_8_BASE_CLOCK | _00_SCI_BAUDRATE_SINGLE | _04_SCI_BIT_MODULATION_ENABLE;

    /* Set bitrate */
    SCI1.BRR = 0x24U;
    SCI1.MDDR = 0xC2U;

    /* Set TXD1 pin */
    MPC.PD3PFS.BYTE = 0x0AU;
    PORTD.PODR.BYTE |= 0x08U;
    PORTD.PDR.BYTE |= 0x08U;
    PORTD.PMR.BYTE |= 0x08U;
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Start
* Description  : This function starts SCI1.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Start(void)
{
    /* Clear interrupt flag */
    IR(SCI1, TXI1) = 0U;

    /* Enable SCI interrupt */
    IEN(SCI1, TXI1) = 1U;
    IEN(SCI1, TEI1) = 1U;
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Stop
* Description  : This function stops SCI1.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Stop(void)
{
    /* Set TXD1 pin */
    PORTD.PMR.BYTE &= 0xF7U;
    SCI1.SCR.BIT.TE = 0U;      /* Disable serial transmit */

    /* Disable SCI interrupt */
    SCI1.SCR.BIT.TIE = 0U;     /* Disable TXI interrupt */
    IEN(SCI1, TXI1) = 0U;
    IEN(SCI1, TEI1) = 0U;
    IR(SCI1, TXI1) = 0U;
}
/***********************************************************************************************************************
* Function Name: R_SCI1_Serial_Send
* Description  : This function transmits SCI1 data.
* Arguments    : tx_buf -
*                    transfer buffer pointer (Not used when transmit data handled by DTC)
*                tx_num -
*                    buffer size (Not used when transmit data handled by DTC or DMAC)
* Return Value : status -
*                    MD_OK or MD_ARGERROR
***********************************************************************************************************************/
MD_STATUS R_SCI1_Serial_Send(uint8_t * const tx_buf, uint16_t tx_num)
{
    MD_STATUS status = MD_OK;

    if (1U > tx_num)
    {
        status = MD_ARGERROR;
    }
    else
    {
        gp_sci1_tx_address = tx_buf;
        g_sci1_tx_count = tx_num;

        /* Set TXD1 pin */
        PORTD.PMR.BYTE |= 0x08U;
        SCI1.SCR.BIT.TIE = 1U;
        SCI1.SCR.BIT.TE = 1U;
    }

    return (status);
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Create
* Description  : This function initializes SCI5.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_Create(void)
{
    /* Cancel SCI5 module stop state */
    MSTP(SCI5) = 0U;

    /* Set interrupt priority */
    IPR(SCI5, ERI5) = _0F_SCI_PRIORITY_LEVEL15;

    /* Clear the control register */
    SCI5.SCR.BYTE = 0x00U;

    /* Initialize SSCL and SSDA pins to high impedance */
    SCI5.SIMR3.BYTE = _C0_SCI_SSCL_HIGH_IMPEDANCE | _30_SCI_SSDA_HIGH_IMPEDANCE;

    /* Set up transfer or reception format in SMR and SCMR */
    SCI5.SMR.BYTE = _00_SCI_CLOCK_PCLK;
    SCI5.SCMR.BIT.SMIF = 0U;
    SCI5.SCMR.BIT.SINV = 0U;
    SCI5.SCMR.BIT.SDIR = 1U;

    /* Set bitrate */
    SCI5.BRR = 0x06U;
    SCI5.MDDR = 0xBFU;

    /* Set control registers */
    SCI5.SEMR.BYTE = _00_SCI_NOISE_FILTER_DISABLE | _04_SCI_BIT_MODULATION_ENABLE;
    SCI5.SIMR1.BYTE |= _01_SCI_IIC_MODE | _00_SCI5_NONE;
    SCI5.SIMR2.BYTE |= _00_SCI_ACK_NACK_INTERRUPTS | _02_SCI_SYNCHRONIZATION | _20_SCI_NACK_TRANSMISSION;
    SCI5.SPMR.BYTE = _00_SCI_CLOCK_NOT_INVERTED | _00_SCI_CLOCK_NOT_DELAYED;
    SCI5.SCR.BYTE = _10_SCI_RECEIVE_ENABLE | _20_SCI_TRANSMIT_ENABLE | _40_SCI_RXI_ERI_ENABLE | _80_SCI_TXI_ENABLE |
                    _04_SCI_TEI_INTERRUPT_ENABLE;

    /* Set SSCL5 pin */
    MPC.PB1PFS.BYTE = 0x0AU;
    PORTB.ODR0.BYTE &= 0xF7U;
    PORTB.ODR0.BYTE |= 0x04U;
    PORTB.PMR.BYTE |= 0x02U;

    /* Set SSDA5 pin */
    MPC.PB2PFS.BYTE = 0x0AU;
    PORTB.ODR0.BYTE &= 0xDFU;
    PORTB.ODR0.BYTE |= 0x10U;
    PORTB.PMR.BYTE |= 0x04U;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Start
* Description  : This function starts SCI5.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_Start(void)
{
    /* Clear interrupt flags */
    IR(SCI5, TXI5) = 0U;
    IR(SCI5, RXI5) = 0U;

    /* Enable TXI and STI interrupt */
    IEN(SCI5, TXI5) = 1U;
    IEN(SCI5, TEI5) = 1U;

    /* Enable RXI interrupt */
    IEN(SCI5, RXI5) = 1U;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_Stop
* Description  : This function stops SCI5.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_Stop(void)
{
    /* Disable TXI and STI interrupt */
    IEN(SCI5, TXI5) = 0U;
    IEN(SCI5, TEI5) = 0U;

    /* Disable RXI interrupt */
    IEN(SCI5, RXI5) = 0U;

    /* Clear interrupt flags */
    IR(SCI5, TXI5) = 0U;
    IR(SCI5, RXI5) = 0U;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_IIC_Master_Send
* Description  : This function sends IIC5 data to slave device.
* Arguments    : adr -
*                    slave device address
*                tx_buf -
*                    transfer buffer pointer (Not used when transmit data handled by DTC or DMAC)
*                tx_num -
*                    buffer size (Not used when transmit data handled by DTC or DMAC)
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_IIC_Master_Send(uint8_t adr, uint8_t * const tx_buf, uint16_t tx_num)
{
    if (1U > tx_num)
    {
        return;
    }

    gp_sci5_tx_address = tx_buf;
    g_sci5_tx_count = tx_num;
    g_sci5_slave_address = adr;
    g_sci5_iic_transmit_receive_flag = _80_SCI_IIC_TRANSMISSION;
    g_sci5_iic_cycle_flag = _80_SCI_IIC_START_CYCLE;

    /* Generate start condition */
    R_SCI5_IIC_StartCondition();
}
/***********************************************************************************************************************
* Function Name: R_SCI5_IIC_Master_Receive
* Description  : This function receives IIC5 data from slave device.
* Arguments    : adr -
*                    slave device address
*                rx_buf -
*                    receive buffer pointer (Not used when receive data handled by DTC or DMAC)
*                rx_num -
*                    buffer size (Not used when receive data handled by DTC or DMAC)
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_IIC_Master_Receive(uint8_t adr, uint8_t * const rx_buf, uint16_t rx_num)
{
    if (1U > rx_num)
    {
        return;
    }

    g_sci5_rx_length = rx_num;
    g_sci5_rx_count = 0;
    gp_sci5_rx_address = rx_buf;
    g_sci5_slave_address = adr;
    g_sci5_iic_transmit_receive_flag = _00_SCI_IIC_RECEPTION;
    g_sci5_iic_cycle_flag = _80_SCI_IIC_START_CYCLE;

    /* Generate start condition */
    R_SCI5_IIC_StartCondition();
}
/***********************************************************************************************************************
* Function Name: R_SCI5_IIC_StartCondition
* Description  : This function generate iic start condition.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_IIC_StartCondition(void)
{
    SCI5.SIMR3.BYTE = _01_SCI_START_CONDITION_ON | _10_SCI_SSDA_START_RESTART_STOP_CONDITION | 
                      _40_SCI_SSCL_START_RESTART_STOP_CONDITION;
}
/***********************************************************************************************************************
* Function Name: R_SCI5_IIC_StopCondition
* Description  : This function generate iic stop condition.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI5_IIC_StopCondition(void)
{
    SCI5.SIMR3.BYTE = _04_SCI_STOP_CONDITION_ON | _10_SCI_SSDA_START_RESTART_STOP_CONDITION | 
                      _40_SCI_SSCL_START_RESTART_STOP_CONDITION;
}

/* Start user code for adding. Do not edit comment generated here */
/***********************************************************************************************************************
* Function Name: R_UART0_Send
* Description  : This function sends UART0 data,and wait until send end.
* Arguments    : tx_buf -
*                    transfer buffer pointer
*                tx_num -
*                    buffer size
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Serial_Send_Data(uint8_t * const tx_buf, uint16_t tx_num)
{
    R_SCI1_Serial_Send(tx_buf,tx_num);
    while(!send_end);           //wait until data transfer end
    send_end = 0;
}

/***********************************************************************************************************************
* Function Name: R_UART0_Display_Start
* Description  : If using windows software to display the picture grabbed by camera,
                 it must send 0x01 and 0xfe at the head.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Serial_Display_Start(void)
{
    unsigned char dis_format_start[2] = {0x01,0xfe};

    R_SCI1_Serial_Send(dis_format_start,2);
    while(!send_end);
    send_end = 0;
}

/***********************************************************************************************************************
* Function Name: R_UART0_Display_End
* Description  : If using windows software to display the picture grabbed by camera,
                 it must send 0xfe and 0x01 at the end.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_SCI1_Serial_Display_End(void)
{
    unsigned char dis_format_end[2] = {0xfe,0x01};

    R_SCI1_Serial_Send(dis_format_end,2);
    while(!send_end);
    send_end = 0;
}
/* End user code. Do not edit comment generated here */
